package controllers;

import dto.SeoConfigDto;
import dto.SitemapDto;
import models.SeoConfig;
import models.crudsiena.SienaSupport;
import play.exceptions.TemplateNotFoundException;
import services.SeoConfigService;

import java.util.Arrays;
import java.util.List;

public class SeoConfigs extends CRUD {
    public static void list(int page, String filterField, String filterValue, String orderBy, String order) {
        CRUD.ObjectType type = CRUD.ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        if (page < 1) {
            page = 1;
        }

        List<? extends SienaSupport> objects = type.findPage(page, filterField, filterValue, orderBy, order);
        Long count = type.count(filterField, filterValue);
        Long totalCount = type.count(null, null);
        try {
            render(type, objects, count, totalCount, page, orderBy, order);
        } catch (TemplateNotFoundException e) {
            render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
        }
    }

    public static void save(SeoConfigDto dto) throws Exception {
        SeoConfigService.merge(dto);
        redirect("SeoConfigs.list");
    }

    public static void input(Long id) {
        SeoConfig model = (id == null) ? new SeoConfig() : SeoConfig.getByKey(SeoConfig.class, id);
        SeoConfigDto dto = SeoConfigService.createDto(model);
        List<SitemapDto> sitemap = Arrays.asList(SitemapDto.all);
        render(dto, sitemap);
    }

    public static void show(String id) {
        redirect("SeoConfigs.input", id);
    }

}
